﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace LogicCircuit
{
    public class LogicCircuit
    {
        private List<Connection> Connections = new List<Connection>();
        private List<Element> Elements = new List<Element>();

        //public void Calculate()
        //{
        //    List<Connection> searchList = new List<Connection>();
        //    searchList.AddRange(Connections);
        //    while (searchList.Count > 0)
        //    {
        //        List<Connection> tempList = new List<Connection>();
        //        tempList.AddRange(Connections);
        //        foreach (Connection c in tempList)
        //        {
        //            if (c.Value != -1)
        //                searchList.Remove(c);
        //        }
        //    }

        //}
        public void Calculate()
        {
            foreach(Connection c in Connections)
            {
                int a = c.Value;
            }
        }
        public void Draw()
        {
            foreach (Connection c in Connections)
            {
                int a = c.Value;
            }
        }
        public void AddConnection(int supX, int supY, int tarX, int tarY)
        {
            //Element size: 80x60
            //Clickbox size: 20x10
            //Element X=0, Y=0;
            //Output clickboxbox: X=60, Y=25 
            //Input clickbox: X=0, Y=25
            //InputA clickbox: X=0, Y=10 
            //InputB clickbox: X=0, Y=40 
            
            //Element supplier = Elements.Find(
            //    delegate(Element el)
            //    {
            //        return ((supX >= el.X + 60 && supX <= el.X + 80) && (supY >= el.Y + 25 && supY <= el.Y + 35));
            //    }
            //    );
            Element supplier = Elements.Find(
                delegate(Element el)
                {
                    return (el.X == supX && el.Y == supY);
                }
                );

            Element target = Elements.Find(
                delegate(Element el)
                {
                    return (el.X == tarX && el.Y == tarY);
                }
                );
            if (target != null && supplier != null)
            {

                Connection c = new Connection(supplier, target);
                if (target.GetType() == typeof(And))
                {
                    ((And)target).InputA = c;
                }
                else if (target.GetType() == typeof(Or))
                {
                    ((Or)target).InputA = c;
                }
                else if (target.GetType() == typeof(Not))
                    ((Not)target).Input = c;
                else if (target.GetType() == typeof(Sink))
                    ((Sink)target).Input = c;
                Connections.Add(c);
            }
        }

        public void AddElement(int x, int y, int type)
        {
            Element element;
            switch (type)
            {
                case 0: element = new Source(x, y); Elements.Add(element); break;
                case 1: element = new And(x, y); Elements.Add(element); break;
                case 2: element = new Or(x, y); Elements.Add(element); break;
                case 3: element = new Not(x, y); Elements.Add(element); break;
                case 4: element = new Sink(x, y); Elements.Add(element); break;
            }

        }

        public void RefreshListBoxes(ListBox list_elements, ListBox list_connections)
        {

            list_elements.Items.Clear();
            list_connections.Items.Clear();

            foreach(Element elem in Elements)
            {
                string line = "";
                if (elem.GetType() == typeof(Source))
                    line += "Source" + "\tx:" + ((Source)elem).X + "\ty:" + ((Source)elem).Y + "\toutput:" + ((Source)elem).Output;
                else if (elem.GetType() == typeof(And))
                    line += "And" + "\tx:" + ((And)elem).X + "\ty:" + ((And)elem).Y + "\toutput:" + ((And)elem).Output;
                else if (elem.GetType() == typeof(Or))
                    line += "Or" + "\tx:" + ((Or)elem).X + "\ty:" + ((Or)elem).Y + "\toutput:" + ((Or)elem).Output;
                else if (elem.GetType() == typeof(Not))
                    line += "Not" + "\tx:" + ((Not)elem).X + "\ty:" + ((Not)elem).Y + "\toutput:" + ((Not)elem).Output;
                else if (elem.GetType() == typeof(Sink))
                {
                    string result;
                    result = (((Sink)elem).Input != null) ? ((Sink)elem).Input.Value.ToString() : "null";
                    line += "Sink" + "\tx:" + ((Sink)elem).X + "\ty:" + ((Sink)elem).Y + "\tRESULT:" + result;
                }
                list_elements.Items.Add(line);
            }
            foreach (Connection c in Connections)
            {
                string line = "";
                line += "supplier: " + c.supplier;
                line += "\ttarget: " + c.target;
                line += "\tvalue: " + c.Value;
                list_connections.Items.Add(line);
            }

        }
    }
}
